#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/io.h>
#include "parport.h"
#include "ppacmd.h"

#define PPORT    0x378

int main()
{
    static uint8_t out[1024]={0};

    printf("Starting test tool on port 0x%x\n", PPORT);

    if (ioperm(PPORT, 5, 1))
    {
        perror("ioperm");
        return 0;
    }

    write_control_port(PPORT, 0);
    write_control_port(PPORT, 4);
    write_control_port(PPORT, 13);

    if (LogiNegociation(PPORT, NEGO_NIBBLE))
    {
        int i = 0;
        
        while (read_nibble(PPORT, &out[i]))
        {
            i++;
        }
        printf("Device id : %s\n", &out[2]);
        
        write_control_port(PPORT, 12);
        write_control_port(PPORT, 14);
        write_control_port(PPORT, 12);

    }
    
    if (LogiNegociation(PPORT, NEGO_EPP))
    {
        //DumpRegisters(PPORT);
        
        ReadPPAID(PPORT);
        ReadScannerID(PPORT);
        ResetHardware(PPORT);
        
        TestCommunication(PPORT);
        IsPowerPlugged(PPORT);
        
        SwitchLight(PPORT, true);
        sleep(2);
        SwitchLight(PPORT, false);
        

        SetMotorSpeed(PPORT, 5, false);
        sleep(2);
        /*  stop motor */
        SetMotorSpeed(PPORT, 31, true);

    
        write_control_port(PPORT, 0);
        write_control_port(PPORT, 4);
        write_control_port(PPORT, 12);

    }
    
    return 1;
}

